/**
 * 开奖结果页面
 */
var resultUI = function() {
    var self = this;

    self.phone = false;
    self.table = null;
    self.dateInput = null;

    self.drawNumberFunc = null;
    self.DrawLongFunc = null;

    self.game = null;
    self.admin = false;
};
resultUI.prototype = {};

resultUI.prototype.init = function() {
    var self = this;
    if (self.dateInput) {
        self.dateInput.change(function() {
            self.query();
        })
    }
    self.query();
};

resultUI.prototype.query = function() {
    var self = this;
    var start = null, end = null;
    if (self.dateInput) {
        start = self.dateInput.val();
        if (start == '今天') {
            start = new Date().Format("yyyy-MM-dd");
        }
    }
    if (start) {
        start = new Date(start).getTime() - 8 * 3600 * 1000;//Format("yyyy-MM-dd");
        end = start + 24 * 3600 * 1000;
    }

    Util.queryTemplate($(".lottery-result tbody"), ".zxf_pagediv", $(".button button"), function(page, size, cb) {
        var data = {
            pageNum: page,
            pageSize: size || 20,
            type: self.game,
        };
        if (self.game == 'lhc' && start) {
            start = null;
            end = null;
        }
        if (start) data.start = start;
        if (end) data.end = end;
        
        startLoading();
        CMD.lottery_query(data, function(r) {
            stopLoading();
            cb(r);
        });
    }, function(table, list, sum) {
        self.fillResult(list);
    });
}

resultUI.prototype.fillResult = function(list) {
    var self = this;
    if (!self.table) return;

    self.table.empty();
    for (var k in list) {
        var o = list[k];
        var tr = $("<tr></tr>");
        if (!self.phone) {
            tr.append("<td class='blue'>" + o.issue + "</td>");
            if (o.time)
                tr.append("<td>" + o.time.substring(5) + "</td>");
            else
                tr.append("<td></td>");
            for (var i in o.red) {
                var c = self.drawNumberFunc(i, o.red[i]);
                tr.append("<td>" + c + "</td>");
            }

            self.DrawLongFunc(o.red, tr);

            if (self.admin) {
                var s = "<td>";
                if (Rights.check("/bet/reopen", context.user.user.grade || '1'))
                    s += "<a href='#' class='reopen' value='__i__'>重新开奖</a> | ";
                if (Rights.check("/bet/cancel", context.user.user.grade || '1'))
                    s += "<a href='#' class='rollback' value='__i__'>撤单</a>";
                s += "</td>";
                tr.append(s.replace(/__i__/g, o.issue));
            }
        }
        self.table.append(tr);
    }

    if (self.admin) {
        $(".rollback").click(function(e) {
            if (!confirm("撤单后，本期所有的注单将被取消！点击确定进行撤单！请慎重操作！")) return;
            startLoading();
            CMD.bet_cancel(self.game, $(e.currentTarget).attr("value"), function(r) {
                stopLoading();
                alert2("撤单完成！");
            });
        });
        $(".reopen").click(function(e) {
            if (!confirm("系统首先会撤销本期注单，并重新等额下注、开奖！请慎重操作！")) return;
            startLoading();
            CMD.bet_reopen(self.game, $(e.currentTarget).attr("value"), function(r) {
                stopLoading();
                alert2("重新开奖完成！");
            });
        });
    }
}

/**
 * PK10
 */
resultUI.prototype.drawNumberPK10 = function(index, n) {
    var c = '<div class="car car__index__"><img src="../public/all/images/__i__.png" width="22px;"/></div>';
    c = c.replace(/__index__/, parseInt(n));
    c = c.replace(/__i__/, n);
    return c;
}
resultUI.prototype.drawLongPK10 = function(red, container) {
    // 冠亚军和
    var gy = parseInt(red[0]) + parseInt(red[1]);
    container.append("<td>" + gy + "</td>");
    if (gy > 11) container.append("<td class='red'>大</td>");
    else container.append("<td>小</td>");
    if (gy%2 == 0) container.append("<td class='red'>双</td>");
    else container.append("<td>单</td>");

    // 1~5龙虎
    for (var i = 0; i < 5; i++) {
        var v0 = parseInt(red[i]), v1 = parseInt(red[9 - i]);
        if (v0 > v1) container.append("<td class='red'>龙</td>");
        else container.append("<td>虎</td>");
    }
}
resultUI.prototype.phoneLongPK10 = function(red) {
    var c = "";
    var f = function(v) {
        c += '<div class="item">' + v + '</div>';
    };

    // 冠亚军和
    var gy = parseInt(red[0]) + parseInt(red[1]);
    f(gy);

    if (gy > 11) f("大");
    else f("小");
    if (gy%2 == 0) f("双");
    else f("单");

    // 1~5龙虎
    for (var i = 0; i < 5; i++) {
        var v0 = parseInt(red[i]), v1 = parseInt(red[9 - i]);
        if (v0 > v1) f("龙");
        else f("虎");
    }
    return c;
}

/**
 * 快乐8
 */
resultUI.prototype.drawNumberEight = function(index, n) {
    var c = '<div class="lottery-number __c__">__i__</div>';
    c = c.replace(/__c__/, parseInt(n) > 40 ? 'big' : 'small');
    c = c.replace(/__i__/, n);
    return c;
}
resultUI.prototype.drawLongEight = function(red, container) {
    // 总和
    var self = this;
    var gameRule = Util.getGameRule(self.game);
    var he = gameRule.he(red);
    container.append("<td>" + he + "</td>");
    if (gameRule.isHeBig(red)) container.append("<td class='red'>大</td>");
    else if (gameRule.isHeSmall(red)) container.append("<td>小</td>");
    else container.append("<td class='blue'>和</td>");

    if (he % 2 ==  0) container.append("<td class='red'>双</td>");
    else container.append("<td>单</td>");

    if (gameRule.isJin(red)) container.append("<td>金</td>");
    else if (gameRule.isMu(red)) container.append("<td>木</td>");
    else if (gameRule.isShui(red)) container.append("<td>水</td>");
    else if (gameRule.isHuo(red)) container.append("<td>火</td>");
    else if (gameRule.isTu(red)) container.append("<td>土</td>");

    if (gameRule.isQianDuo(red)) container.append("<td class='red'>前多</td>");
    else if (gameRule.isHouDuo(red)) container.append("<td>后多</td>");
    else container.append("<td class='blue'>前后和</td>");

    if (gameRule.isDanDuo(red)) container.append("<td>单多</td>");
    else if (gameRule.isShuangDuo(red)) container.append("<td class='red'>双多</td>");
    else container.append("<td class='blue'>单双和</td>");
}
resultUI.prototype.phoneLongEight = function(red) {
    var self = this;
    var c = "";
    var f = function(v) {
        c += '<div class="item">' + v + '</div>';
    };

    // 总和
    var he = gameRule.he(red);
    f(he);

    if (gameRule.isHeBig(red)) f("大");
    else if (gameRule.isHeSmall(red)) f("小");
    else f("和");

    if (he % 2 ==  0) f("双");
    else f("单");

    if (gameRule.isJin(red)) f("金");
    else if (gameRule.isMu(red)) f("木");
    else if (gameRule.isShui(red)) f("水");
    else if (gameRule.isHuo(red)) f("火");
    else if (gameRule.isTu(red)) f("土");

    if (gameRule.isQianDuo(red)) f("前多");
    else if (gameRule.isHouDuo(red)) f("后多");
    else f("前后和");

    if (gameRule.isDanDuo(red)) f("单多");
    else if (gameRule.isShuangDuo(red)) f("双多");
    else f("单双和");
    return c;
}

/**
 * 快乐十分
 */
resultUI.prototype.drawNumberCqTen = function(index, n) {
    var c = '<div class="lottery-number-ten"><img src="../public/all/images/fruit___i__.png"/></div>';
    c = c.replace(/__index__/, parseInt(n));
    c = c.replace(/__i__/, parseInt(n));
    return c;
}
resultUI.prototype.drawNumberTen = function(index, n) {
    var c = '<div class="lottery-number"><img src="../public/all/images/__i__.png"/></div>';
    c = c.replace(/__index__/, parseInt(n));
    c = c.replace(/__i__/, n);
    return c;
}
resultUI.prototype.drawLongTen = function(red, tr) {
    // 总和
    var self = this;
    var gameRule = Util.getGameRule(self.game);
    var he = gameRule.he(red);
    tr.append("<td>" + he + "</td>");
    if (gameRule.isHeBig(red)) tr.append("<td class='red'>大</td>");
    else if (gameRule.isHeSmall(red)) tr.append("<td>小</td>");
    else tr.append("<td class='blue'>和</td>");

    if (!gameRule.isOdd(he)) tr.append("<td class='red'>双</td>");
    else tr.append("<td>单</td>");
    if (gameRule.isWeiDa(he)) tr.append("<td class='red'>尾大</td>");
    else tr.append("<td>尾小</td>");

    // 1~4龙虎
    for (var i = 0; i < 4; i++) {
        if (gameRule.isLong(red, i)) tr.append("<td class='red'>龙</td>");
        else tr.append("<td>虎</td>");
    }
}
resultUI.prototype.phoneLongTen = function(red) {
    var c = "";
    var f = function(v) {
        c += '<div class="item">' + v + '</div>';
    };

    // 总和
    var he = gameRule.he(red);
    f(he);
    if (gameRule.isHeBig(red)) f("大");
    else if (gameRule.isHeSmall(red)) f("小");
    else f("和");

    if (!gameRule.isOdd(he)) f("双");
    else f("单");
    if (gameRule.isWeiDa(he)) f("尾大");
    else f("尾小");

    // 1~4龙虎
    for (var i = 0; i < 4; i++) {
        if (gameRule.isLong(red, i)) f("龙");
        else f("虎");
    }
    return c;
}

/**
 * 时时彩
 */
resultUI.prototype.drawNumberSsc = function(index, n) {
    var c = '<div class="lottery-number"><img src="../public/all/images/__i__.png"/></div>';
    c = c.replace(/__index__/, parseInt(n));
    c = c.replace(/__i__/, n);
    return c;
}
resultUI.prototype.drawLongSsc = function(red, tr) {
    var self = this;
    var gameRule = Util.getGameRule(self.game);

    // 总和
    var he = gameRule.he(red);
    tr.append("<td>" + he + "</td>");
    if (gameRule.isHeBig(red)) tr.append("<td class='red'>大</td>");
    else tr.append("<td>小</td>");
    if (gameRule.isHeOdd(red)) tr.append("<td>单</td>");
    else tr.append("<td class='red'>双</td>");

    // 龙虎
    if (gameRule.isLong(red)) 
        tr.append("<td class='red'>龙</td>");
    else if (gameRule.isHu(red))
        tr.append("<td>虎</td>");
    else if (gameRule.isHe(red))
        tr.append("<td class='blue'>和</td>");

    // 前三
    if (gameRule.isQian3BaoZi(red))
        tr.append("<td>豹子</td>");
    else if (gameRule.isQian3ShunZi(red))
        tr.append("<td>顺子</td>");
    else if (gameRule.isQian3DuiZi(red))
        tr.append("<td>对子</td>");
    else if (gameRule.isQian3BanShun(red))
        tr.append("<td>半顺</td>");
    else if (gameRule.isQian3ZaLiu(red))
        tr.append("<td>杂六</td>");

    // 中三
    if (gameRule.isZhong3BaoZi(red))
        tr.append("<td>豹子</td>");
    else if (gameRule.isZhong3ShunZi(red))
        tr.append("<td>顺子</td>");
    else if (gameRule.isZhong3DuiZi(red))
        tr.append("<td>对子</td>");
    else if (gameRule.isZhong3BanShun(red))
        tr.append("<td>半顺</td>");
    else if (gameRule.isZhong3ZaLiu(red))
        tr.append("<td>杂六</td>");

    // 后三
    if (gameRule.isHou3BaoZi(red))
        tr.append("<td>豹子</td>");
    else if (gameRule.isHou3ShunZi(red))
        tr.append("<td>顺子</td>");
    else if (gameRule.isHou3DuiZi(red))
        tr.append("<td>对子</td>");
    else if (gameRule.isHou3BanShun(red))
        tr.append("<td>半顺</td>");
    else if (gameRule.isHou3ZaLiu(red))
        tr.append("<td>杂六</td>");
}
resultUI.prototype.phoneLongSsc = function(red) {
    var c = "";
    var f = function(v) {
        c += '<div class="item">' + v + '</div>';
    };

    // 总和
    var he = gameRule.he(red);
    f(he);

    if (gameRule.isHeBig(red)) f("大");
    else f("小");
    if (gameRule.isHeOdd(red)) f("单");
    else f("双");

    // 龙虎
    if (gameRule.isLong(red)) 
        f("龙");
    else if (gameRule.isHu(red))
        f("虎");
    else if (gameRule.isHe(red))
        f("和");

    // 前三
    if (gameRule.isQian3BaoZi(red))
        f("豹子");
    else if (gameRule.isQian3ShunZi(red))
        f("顺子");
    else if (gameRule.isQian3DuiZi(red))
        f("对子");
    else if (gameRule.isQian3BanShun(red))
        f("半顺");
    else if (gameRule.isQian3ZaLiu(red))
        f("杂六");

    // 中三
    if (gameRule.isZhong3BaoZi(red))
        f("豹子");
    else if (gameRule.isZhong3ShunZi(red))
        f("顺子");
    else if (gameRule.isZhong3DuiZi(red))
        f("对子");
    else if (gameRule.isZhong3BanShun(red))
        f("半顺");
    else if (gameRule.isZhong3ZaLiu(red))
        f("杂六");

    // 后三
    if (gameRule.isHou3BaoZi(red))
        f("豹子");
    else if (gameRule.isHou3ShunZi(red))
        f("顺子");
    else if (gameRule.isHou3DuiZi(red))
        f("对子");
    else if (gameRule.isHou3BanShun(red))
        f("半顺");
    else if (gameRule.isHou3ZaLiu(red))
        f("杂六");
    return c;
}

/**
 * 11选5
 */
resultUI.prototype.drawNumber115 = function(index, n) {
    var c = '<div class="lottery-number"><img src="../public/all/images/__i__.png"/></div>';
    c = c.replace(/__index__/, parseInt(n));
    c = c.replace(/__i__/, n);
    return c;
}
resultUI.prototype.drawLong115 = function(red, tr) {
    // 总和
    var self = this;
    var gameRule = Util.getGameRule(self.game);
    // 总和
    var he = gameRule.he(red);
    tr.append("<td>" + he + "</td>");
    if (gameRule.isHeBig(red))
        tr.append("<td class='red'>大</td>");
    else if (gameRule.isHeSmall(red))
        tr.append("<td>小</td>");
    else tr.append("<td> </td>");
    if (gameRule.isHeOdd(red))
        tr.append("<td>单</td>");
    else
        tr.append("<td class='red'>双</td>");
    if (gameRule.isHeWeiBig(red))
        tr.append("<td class='red'>尾大</td>");
    else
        tr.append("<td>尾小</td>");

    // 龙虎
    if (gameRule.isLong(red))
        tr.append("<td class='red'>龙</td>");
    else if (gameRule.isHu(red))
        tr.append("<td>虎</td>");
    else if (gameRule.isHe(red))
        tr.append("<td class='blue'>和</td>");

    // 1~5大小
    for (var i = 0; i < 5; i++) {
        var v = parseInt(red[i]);
        if (gameRule.isBig(v)) tr.append("<td class='red'>大</td>");
        else if (gameRule.isSmall(v)) tr.append("<td>小</td>");
        else tr.append("<td class='blue'>和</td>");
    }
}
resultUI.prototype.phoneLong115 = function(red) {
    var c = "";
    var f = function(v) {
        c += '<div class="item">' + v + '</div>';
    };

    // 总和
    var he = gameRule.he(red);
    f(he);

    if (gameRule.isHeBig(red))
        f("大");
    else if (gameRule.isHeSmall(red))
        f("小");
    if (gameRule.isHeOdd(red))
        f("单");
    else
        f("双");
    if (gameRule.isHeWeiBig(red))
        f("尾大");
    else
        f("尾小");

    // 龙虎
    if (gameRule.isLong(red))
        f("龙");
    else if (gameRule.isHu(red))
        f("虎");
    else if (gameRule.isHe(red))
        f("和");

    // 1~5大小
    for (var i = 0; i < 5; i++) {
        var v = parseInt(red[i]);
        if (gameRule.isBig(v)) f("大");
        else if (gameRule.isSmall(v)) f("小");
        else f("和");
    }
    return c;
}

/**
 * 快3
 */
resultUI.prototype.drawNumberK3 = function(index, n) {
    var c = '<div class="lottery-number-k3"><img src="../public/all/images/dice___i__.png"/></div>';
    c = c.replace(/__index__/, parseInt(n));
    c = c.replace(/__i__/, parseInt(n));
    return c;
}
resultUI.prototype.drawLongK3 = function(red, tr) {
    var self = this;
    var gameRule = Util.getGameRule(self.game);

    var he = gameRule.he(red);
    tr.append("<td>" + he + "</td>");
    if (gameRule.isBaoZi(red)) tr.append("<td class='blue'>豹子</td>");
    else if (gameRule.isHeBig(red)) tr.append("<td class='red'>大</td>");
    else tr.append("<td>小</td>");
}
resultUI.prototype.phoneLongK3 = function(red) {
    var c = "";
    var f = function(v) {
        c += '<div class="item">' + v + '</div>';
    };

    // 总和
    var he = gameRule.he(red);
    f(he);
    
    if (gameRule.isBaoZi(red)) f("豹子");
    else if (gameRule.isHeBig(red)) f("大");
    else f("小");
    return c;
}

/**
 * 六合彩
 */
resultUI.prototype.drawNumberLhc = function(index, n) {
    var self = this;
    var gameRule = Util.getGameRule(self.game);
    var xiao = gameRule.getShengXiao(parseInt(n));
    var c = '<div class="lottery-number-lhc __class__">__i__</div>__xiao__';
    c = c.replace(/__xiao__/, xiao);
    c = c.replace(/__i__/, n);
    n = parseInt(n);
    if (gameRule.hongbo().indexOf(n) != -1) c = c.replace(/__class__/, 'item-red');
    else if (gameRule.lanbo(n).indexOf(n) != -1) c = c.replace(/__class__/, 'item-blue');
    else if (gameRule.lvbo(n).indexOf(n) != -1) c = c.replace(/__class__/, 'item-green');
    return c;
}
resultUI.prototype.drawLongLhc = function(red, tr) {
    var self = this;
    var gameRule = Util.getGameRule(self.game);

    // 总和
    var he = gameRule.he(red);
    tr.append("<td>" + he + "</td>");

    // 单双
    if (!gameRule.heOdd(red)) tr.append("<td class='red'>双</td>");
    else tr.append("<td class=''>单</td>");

    // 大小
    if (gameRule.heBig(red))  tr.append("<td class='red'>大</td>");
    else tr.append("<td class=''>小</td>");

    // 七色波
    var qi = gameRule.qiSeBo(red);
    if (qi == '红') tr.append("<td class='red'>红</td>");
    else if (qi == '蓝') tr.append("<td class='blue'>蓝</td>");
    else if (qi == '绿') tr.append("<td class='green'>绿</td>");
    else tr.append("<td class=''>和</td>");

    // 特码
    var te = parseInt(red[6]);
    if (te % 2 == 0) tr.append("<td class='red'>双</td>");
    else tr.append("<td class=''>单</td>");
    if (te >= 25) tr.append("<td class='red'>大</td>");
    else tr.append("<td class=''>小</td>");

    var he = Math.floor(te / 10) + (te % 10);
    if (te == 49) tr.append("<td class='blue'>和</td>")
    else if (he % 2 == 0) tr.append("<td class='red'>双</td>");
    else tr.append("<td class=''>单</td>");
    if (te == 49) tr.append("<td class='blue'>和</td>")
    else if (he >= 7) tr.append("<td class='red'>大</td>");
    else tr.append("<td class=''>小</td>");

    var wei = te % 10;
    if (wei >= 5) tr.append("<td class='red'>大</td>");
    else tr.append("<td class=''>小</td>");
}
resultUI.prototype.phoneLongLhc = function(red) {
    var c = "";
    var f = function(v) {
        c += '<div class="item">' + v + '</div>';
    };

    // 总和
    var he = gameRule.he(red);
    f(he);

    // 单双
    if (!gameRule.heOdd(red)) f("双");
    else f("单");

    // 大小
    if (gameRule.heBig(red))  f("大");
    else f("小");

    // 七色波
    var qi = gameRule.qiSeBo(red);
    if (qi == '红') f("红");
    else if (qi == '蓝') f("蓝");
    else if (qi == '绿') f("绿");
    else f("和");

    // 特码
    var te = parseInt(red[6]);
    if (te % 2 == 0) f("双");
    else f("单");
    if (te >= 25) f("大");
    else f("小");

    var he = Math.floor(te / 10) + (te % 10);
    if (te == 49) f("和")
    else if (he % 2 == 0) f("双");
    else f("单");
    if (te == 49) f("和")
    else if (he >= 7) f("大");
    else f("小");

    var wei = te % 10;
    if (wei >= 5) f("大");
    else f("小");
    return c;
}

window.ResultUI = new resultUI();